<template>
  <div class="container">
    <!-- 轮播图 -->
    <el-carousel trigger="click" height="630px" :interval="3000">
      <el-carousel-item v-for="(item, index) in banners" :key="index">
        <div
          class="banner-image"
          :style="`background:url(${item.url})center no-repeat; background-size:contain contain;`"
        ></div>
      </el-carousel-item>
    </el-carousel>
    <!-- 搜索框 -->
    <div class="banner-content">
      <div class="search-bar">
        <!-- tab栏 -->
        <el-row type="flex" class="search-tab">
          <span
            v-for="(item, index) in options"
            :key="index"
            :class="{ active: index === currentOption }"
            @click="handleOption(index)"
          >
            <i>{{ item.name }}</i>
          </span>
        </el-row>
        <!-- 输入框 -->
        <el-row type="flex" align="middle" class="search-input">
          <input
            :placeholder="options[currentOption].placeholder"
            v-model="searchValue"
            @keyup.enter="handleSearch"
          />
          <i class="el-icon-search" @click="handleSearch"></i>
        </el-row>
      </div>
    </div>
  </div>
</template>

<script>
import { banners } from "@/utils/indexApi";
export default {
  async mounted() {
    // 这个是在已进入页面输入框就自动获取焦点
    document.querySelector("input").focus();
    const res = await banners();
    const { data } = res.data;
    this.banners = data.map((img) => {
      img.url = res.config.baseURL + img.url;
      return img;
    });
  },
  data() {
    return {
      // 获取首页轮播图的数据
      banners: [],
      options: [
        // 搜索框tab选项
        {
          name: "攻略",
          placeholder: "搜索城市",
          pageUrl: "/post?city=",
        },
        {
          name: "酒店",
          placeholder: "请输入城市搜索酒店",
          pageUrl: "/hotel?city=",
        },
        {
          name: "机票",
          placeholder: "请输入出发地",
          pageUrl: "/air",
        },
      ],
      searchValue: "", // 搜索框的值
      currentOption: 0, // 当前选中的选项
    };
  },

  methods: {
    handleOption(index) {
      this.currentOption = index;
      const item = this.options[index];
      // 判断 如果是点击了机票就跳转到机票的页面
      if (item.name == "机票") {
        this.$router.push(item.pageUrl);
      }
    },

    handleSearch() {
      const item = this.options[this.currentOption];
      // 跳转时候给对应的页面url加上搜索内容参数
      this.$router.push(
        this.options[this.currentOption].pageUrl + this.searchValue
      );
      // this.$router.push(item.pageUrl + this.searchValue);
    },
  },
};
</script>

<style lang="less" scoped>
.container {
  min-width: 1000px;
  margin: 0 auto;
  position: relative;

  /deep/ .el-carousel__container {
    height: 700px;
  }

  .banner-image {
    width: 100%;
    height: 100%;
  }

  .banner-content {
    position: absolute;
    top: 40%;
    left: 50%;
    z-index: 9;
    width: 1000px;
    margin-left: -500px;
    border-top: 1px transparent solid;

    .search-bar {
      width: 552px;
      margin: 0 auto;

      .search-tab {
        .active {
          i {
            color: #333;
          }
          // 添加伪元素
          &::after {
            background-color: #fff;
          }
        }
      }

      span {
        width: 82px;
        height: 36px;
        display: block;
        position: relative;
        margin-right: 8px;
        cursor: pointer;

        i {
          position: absolute;
          z-index: 2;
          display: block;
          width: 100%;
          height: 100%;
          line-height: 36px;
          text-align: center;
          color: #fff;
        }

        &::after {
          content: "";
          position: absolute;
          left: 0;
          top: 0;
          display: block;
          height: 100%;
          width: 100%;
          background: rgba(0, 0, 0, 0.5);
          transform: scale(1.1, 1.2) perspective(0.4em) rotateX(2.2deg);
          border: 1px solid rgba(255, 255, 255, 0.3);
          transform-origin: bottom left;
          border-radius: 1px 10px 0 0;
          box-sizing: border-box;
        }
      }
    }
  }

  .search-input {
    width: 500px;
    height: 46px;
    background-color: #fff;
    padding-left: 20px;
    border-top: none;
    input {
      border: none;
      padding-left: 10px;
      flex: 1;
      font-size: 16px;
      outline: none;
    }

    .el-icon-search {
      cursor: pointer;
      padding: 0 10px;
      font-size: 20px;
      font-weight: 700;
    }
  }
}
</style>